<!-- 车型管理 -->
<template>
  <div
    class="dashboard-container car-models customer-list-box"
    style="width: 100%"
  >
    <div class="app-container">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="carList"
          label-width="80px"
          :model="carList"
        >
          <div class="filter-container">
            <el-row :gutter="60">
              <el-col :span="8">
                <el-form-item
                  label="车型编号:"
                  style="margin-bottom: 0px"
                  label-width="75px"
                >
                  <el-input
                    v-model="carList.id"
                    placeholder="请输入车型编号"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="应载重量:"
                  style="margin-bottom: 20px"
                  label-width="110px"
                >
                  <el-select
                    v-model="carList.allowableLoad"
                    clearable
                    placeholder="请选择应载重量"
                  >
                    <el-option
                      v-for="item in carModelsWeight"
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                    >{{ item.name }}</el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="应载体积:"
                  style="margin-bottom: 20px"
                  label-width="110px"
                >
                  <el-select
                    v-model="carList.allowableVolume"
                    clearable
                    placeholder="请选择应载体积"
                  >
                    <el-option
                      v-for="item in carModelsVolume"
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                    >{{ item.name }}</el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  class="input_4"
                  label="车辆类型:"
                  style="margin-bottom: 20px"
                  label-width="75px"
                >
                  <el-input
                    v-model="carList.name"
                    placeholder="请输入车辆类型"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button
                  type="warning"
                  @click="search"
                >搜索</el-button>
                <el-button
                  class="reset-btn"
                  plain
                  @click="res"
                >{{ mingzi }}</el-button>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            style="float: left; margin-bottom:20px;width:100px"
            type="warning"
            @click="add"
          >新增车型</el-button>
          <el-table
            ref="multipleTable"
            :data="dataList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <!-- 没有的时候显示 -->
            <div
              v-show="(!dataList || dataList.length <= 0)"
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="(!dataList || dataList.length <= 0)"
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column

              type="index"
              width="80"
              label="序号"
            >
              <template slot-scope="scope">
                <span>{{
                  scope.$index + (waybillSearchFormData.page - 1) * waybillSearchFormData.pageSize + 1
                }}</span>
              </template>
            </el-table-column>
            <el-table-column

              label="车型编号"
              width="180"
              prop="id"
            >
            </el-table-column>
            <el-table-column

              label="车辆类型"
              width="120"
              prop="name"
            >
            </el-table-column>
            <el-table-column

              label="车型数量"
              min-width="120px"
              prop="num"
            >
            </el-table-column>
            <el-table-column

              label="应载重量(吨)"
              min-width="120px"
              prop="allowableLoad"
            >
            </el-table-column>
            <el-table-column

              label="应载体积(立方体)"
              width="130"
              prop="allowableVolume"
            >
            </el-table-column>
            <el-table-column

              label="长(米)"
              min-width="120px"
              prop="measureLong"
            >
            </el-table-column>
            <el-table-column

              label="宽(米)"
              min-width="120px"
              prop="measureWidth"
            >
            </el-table-column>
            <el-table-column

              label="高(米)"
              min-width="120px"
              prop="measureHigh"
            >
            </el-table-column>
            <el-table-column
              fixed="right"
              align="center"
              label="操作"
              width="120"
            >
              <template v-slot="{ row }">
                <el-button
                  type="text"
                  size="49"
                  @click="edit(row)"
                >编辑</el-button>
                <span class="xian"> | </span>
                <el-button
                  type="text"
                  size="49"
                  class="del"
                  style="color:#f56c6c"
                  @click="del(row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div
            v-show="dataList && dataList.length > 0"
            class="pagination"
          >
            <div class="pages">
              <el-pagination
                :current-page="Number(waybillSearchFormData.page)"
                :total="Number(total)"
                :page-size="Number(waybillSearchFormData.pageSize)"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <CarModelsAdd
      ref="CarModelsAdd"
      :show-dialog.sync="showDialog"
      :car-models="carModels"
      :title="title"
    />
  </div>
</template>
<script>
import {
  _getCarModelsList,
  _getCarModelsOne,
  _delCarModelsOne
} from '@/api/carModels'
import CarModelsAdd from './components/car-models-add.vue'
export default {
  components: {
    CarModelsAdd
  },
  data() {
    return {
      mingzi: '重置',
      listLoading: true,
      total: null,
      waybillSearchFormData: {
        page: 1,
        pageSize: 10
      },
      title: '',
      carList: {
        allowableLoad: '',
        allowableVolume: '',
        id: '',
        name: ''
      },
      carModels: false,
      showDialog: false,
      carModelsWeight: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      carModelsVolume: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ],
      dataList: []
    }
  },
  created() {
    this.getCarModelsList()
    // 键盘enter操作
    const _this = this
    document.onkeydown = function (e) {
      var key = window.event.keyCode
      if (key === 13) {
        _this.search()
      }
    }
  },
  methods: {
    res() {
      this.mingzi = '重置'
      this.carList = {
        allowableLoad: '',
        allowableVolume: '',
        id: '',
        name: ''
      }
      this.waybillSearchFormData.page = 1
      this.waybillSearchFormData.allowableLoad = ''
      this.waybillSearchFormData.allowableVolume = ''
      this.waybillSearchFormData.id = ''
      this.waybillSearchFormData.name = ''
      this.getCarModelsList()
    },
    search() {
      this.mingzi = '请重置'
      if (
        this.carList.allowableLoad === '' &&
        this.carList.allowableVolume === '' &&
        this.carList.id === '' &&
        this.carList.name === ''
      ) {
        this.mingzi = '重置'
      }
      this.waybillSearchFormData.page = 1
      this.waybillSearchFormData.allowableLoad = this.carList.allowableLoad.trim()
      this.waybillSearchFormData.allowableVolume = this.carList.allowableVolume.trim()
      this.waybillSearchFormData.id = this.carList.id.trim()
      this.waybillSearchFormData.name = this.carList.name.trim()
      // const obj = {
      //   allowableLoad: this.carList.allowableLoad,
      //   allowableVolume: this.carList.allowableVolume,
      //   id: this.carList.id,
      //   name: this.carList.name,
      //   page: 1,
      //   pageSize: this.waybillSearchFormData.pageSize
      // }
      this.getCarModelsList()
    },
    handleSizeChange(val) {
      this.waybillSearchFormData.pageSize = val
      this.waybillSearchFormData.page = 1
      this.getCarModelsList(this.waybillSearchFormData)
    },
    handleCurrentChange(val) {
      this.waybillSearchFormData.page = val
      this.getCarModelsList(this.waybillSearchFormData)
    },
    async getCarModelsList() {
      this.listLoading = true
      const res = await _getCarModelsList(this.waybillSearchFormData)
      this.dataList = res.data.items
      this.total = res.data.counts
      this.listLoading = false
    },
    add() {
      this.title = '新增车型'
      this.showDialog = true
    },
    async edit(row) {
      this.title = '编辑车型'
      this.showDialog = true
      const res = await _getCarModelsOne(row.id)
      this.$refs.CarModelsAdd.carModels = res.data
    },
    del(row) {
      if (row.num) {
        this.$message.warning('此车型类型下有车辆，不能删除')
      } else {
        this.$confirm(`确认删除 车辆类型 ：${row.name} 吗？`, '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          try {
            await _delCarModelsOne(row.id)
            this.$message.success('删除成功')
            this.getCarModelsList()
          } catch (err) {
            this.$message.warning('删除失败')
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.reset-btn {
  width: 90px;
  height: 40px;
}
.input_4 {
  margin-bottom: 0px !important;
}
.el-card__body {
  padding: 20px 20px 20px 0;
}
.add-btn {
  margin-bottom: 20px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }

}
</style>
